let DIR = cc.Enum({
    UP: 0,
    RIGHT: 1,
    DOWN: 2,
    LEFT: 3
});
cc.Class({
    extends: cc.Component,

    properties: {
        bombPrefab: {
            default: null,
            type: cc.Prefab,
        },

        map: {
            default: null,
            type: cc.TiledMap,
        },
        hideLayer: {
            default: null,
            type: cc.TiledLayer,
        },
        mainLayer: {
            default: null,
            type: cc.TiledLayer,
        },
        soilLayer: {
            default: null,
            type: cc.TiledLayer,
        },
        player: {
            default: null,
            type: cc.Node,
        },
        enemy: {
            default: null,
            type: cc.Node,
        }
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start() {
        this.time = 0;
        this.loadMap();
        this.node.on("exploded", function () {
            // 检查炸弹的上下左右是否是soil
            if (this.soilLayer.getTileGIDAt(cc.v2(this.bombTile.x, this.bombTile.y - 1))) {
                // 炸弹上面是soil，可炸
                this.soilLayer.setTileGIDAt(0, cc.v2(this.bombTile.x, this.bombTile.y - 1));
            }
            if (this.soilLayer.getTileGIDAt(cc.v2(this.bombTile.x, this.bombTile.y + 1))) {
                // 炸弹下面是soil，可炸
                this.soilLayer.setTileGIDAt(0, cc.v2(this.bombTile.x, this.bombTile.y + 1));
            }
            if (this.soilLayer.getTileGIDAt(cc.v2(this.bombTile.x - 1, this.bombTile.y))) {
                // 炸弹左面是soil，可炸
                this.soilLayer.setTileGIDAt(0, cc.v2(this.bombTile.x - 1, this.bombTile.y));
            }
            if (this.soilLayer.getTileGIDAt(cc.v2(this.bombTile.x + 1, this.bombTile.y))) {
                // 炸弹右面是soil，可炸
                this.soilLayer.setTileGIDAt(0, cc.v2(this.bombTile.x + 1, this.bombTile.y));
            }
            // 把炸弹移除掉
            this.map.node.removeChild(this.bomb);
        }.bind(this));

        this.moveEnemy();
    },

    btnClick(event, data) {
        switch (data) {
            case 'up':
                this.playerDir = DIR.UP;
                let playerTarTile = cc.v2(this.playerTile.x, this.playerTile.y - 1);
                this.tryMoveToTarTile(playerTarTile);
                break;
            case 'down':
                this.playerDir = DIR.DOWN;
                playerTarTile = cc.v2(this.playerTile.x, this.playerTile.y + 1);
                this.tryMoveToTarTile(playerTarTile);
                break;
            case 'left':
                this.playerDir = DIR.LEFT;
                playerTarTile = cc.v2(this.playerTile.x - 1, this.playerTile.y);
                this.tryMoveToTarTile(playerTarTile);
                break;
            case 'right':
                this.playerDir = DIR.RIGHT;
                playerTarTile = cc.v2(this.playerTile.x + 1, this.playerTile.y);
                this.tryMoveToTarTile(playerTarTile);
                break;
            default:
                break;
        }
    },

    loadMap() {
        // 获取对象层
        let objects = this.map.getObjectGroup('objects');
        // 获取对象
        let playerObj = objects.getObject('player');
        let enemyObj = objects.getObject('enemy');
        // 获取坐标
        let playerPos = cc.v2(playerObj.offset.x, playerObj.offset.y);
        let enemyPos = cc.v2(enemyObj.offset.x, enemyObj.offset.y);
        // 玩家和敌人的瓦片坐标
        this.playerTile = this.getTilePosition(playerPos);
        this.enemyTile = this.getTilePosition(enemyPos);
        // 设置敌人的位置
        let enemyPos2 = this.mainLayer.getPositionAt(this.enemyTile);
        this.enemy.setPosition(enemyPos2);
        this.enemyDir = DIR.RIGHT;

        // 更新玩家的位置
        this.updatePlayerPos();
        this.playerDir = DIR.DOWN;
    },
    updatePlayerPos() {
        let pos = this.mainLayer.getPositionAt(this.playerTile);
        this.player.setPosition(pos);
    },

    updateEnemyPos() {
        let pos = this.mainLayer.getPositionAt(this.enemyTile);
        this.enemy.setPosition(pos);
    },

    //将地图中的坐标转为瓦片的坐标
    getTilePosition(posInPixel) {
        let mapSize = this.node.getContentSize();
        let tileSize = this.map.getTileSize();
        let x = Math.floor(posInPixel.x / tileSize.width);
        let y = Math.floor(posInPixel.y / tileSize.height);
        return cc.v2(x, y);
    },

    tryMoveToTarTile(newTile) {
        // 让玩家别撞到了墙上
        if (this.mainLayer.getTileGIDAt(newTile)) {
            cc.log('hit the wall .');
            return false;
        }
        if (this.soilLayer.getTileGIDAt(newTile)) {
            cc.log('hit the soil .');
            return false;
        }
        // 检查玩家是否和敌人进行了碰撞
        if (newTile.x == this.enemyTile.x && newTile.y == this.enemyTile.y) {
            cc.log('hit the enemy .');
            return false;
        }

        this.playerTile = newTile;
        this.updatePlayerPos();
    },

    moveEnemy() {
        this.schedule(function () {
            // 敌人的逻辑
            if (this.enemyDir == DIR.RIGHT) {
                this.enemyTile.x++;
                if (this.enemyTile.x > 20) {
                    this.enemyDir = DIR.LEFT;
                }
            } else {
                this.enemyTile.x--;
                if (this.enemyTile.x < 2) {
                    this.enemyDir = DIR.RIGHT;
                }
            }
            this.updateEnemyPos();
        }, 1);
    },

    update(dt) {},
});